package com.qk.aspect;

import com.qk.entity.OperateLog;
import com.qk.mapper.OperateLogMapper;
import com.qk.utils.CurrentUserHoler;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class OpeateLogAspect {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Around("@annotation(com.qk.anno.Log)")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
        log.info("记录操作日志");
        OperateLog operate = new OperateLog();
        operate.setOperateUserId(CurrentUserHoler.getCurrentUser());
        operate.setOperateTime(LocalDateTime.now());
        operate.setClassName(joinPoint.getTarget().getClass().getName());
        operate.setMethodName(joinPoint.getSignature().getName());
        operate.setMethodParams(Arrays.toString(joinPoint.getArgs()));
        log.info("操作开始");
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        log.info("操作结束");
        long end = System.currentTimeMillis();
        operate.setReturnValue(result.toString());
        operate.setCostTime(end - start);

        operateLogMapper.insert(operate);
        return result;
    }
}
